package pure.ui.groups 
{
	import pure.ui.skins.ISkin;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	/**
	 * 组件组合基类
	 * @author Des
	 * 
	 */
	
public class GroupBase extends Sprite
{
	/**
	 * @param x				x轴位置
	 * @param y				y轴位置
	 * @param skin			统一皮肤(单项添加无视)
	 **/
	public function GroupBase(x:Number, y:Number, skin:ISkin = null) 
	{
		this.x = x;
		this.y = y;
		this._skin = skin;
		_init();
	}
	
	
	
	/*model*/
	
	protected var _items:Array;	// 组件列表
	
	protected var _skin:ISkin;	// 皮肤
	
	protected var _selectedId:String;	// 被选中id

	/*cache*/
	
	protected var _numItem:int;	// 组件数目

	
	
	/**
	 * 添加Item
	 * @param id
	 * @param x
	 * @param y
	 * @param itemSize
	 * @param skin
	 * @param hasLabel
	 **/
	public function addItem(id:String,x:Number, y:Number,  itemSize:Point = null, skin:ISkin = null,hasLabel:Boolean=true):void
	{
		var item:IGroupItem = _createItem(x, y, itemSize, skin, hasLabel);
		item.id = id;
		_items.push(item);
		_numItem++;
		addChild(DisplayObject(item));
	}
	
	/**
	 * 添加多Item
	 * @param itemsInfo
	 * @param x
	 * @param y
	 * @param itemSize
	 * @param layoutType
	 * @param layoutData
	 * @param hasLabel
	 **/
	public function addItems(itemsInfo:Object,x:Number,y:Number, itemSize:Point = null,layoutType:int = 2,layoutData:Object = 4,hasLabel:Boolean=true):void
	{
		var len:int = (itemsInfo is Array)?itemsInfo.length:int(itemsInfo);
		var item:IGroupItem;
		for (var i:int = 0; i < len; i++)
		{
			if (layoutType == LayoutType.HORIZONTAL)
			{
				item = _createItem(x + i * Number(layoutData), y,itemSize, _skin, hasLabel);
			}
			else if (layoutType == LayoutType.VERTICAL)
			{
				item = _createItem(x , y+i * Number(layoutData),itemSize, _skin,hasLabel);
			}
			else
			{
				item = _createItem( layoutData[i][0], layoutData[i][1],itemSize, _skin,hasLabel);
			}
			item.id = (itemsInfo is Array)?itemsInfo[i]:i.toString();
			_items.push(item);
			addChild(DisplayObject(item));
		}
		_numItem += len;
	}
	
	/**
	 * 获取指定id的组合组件
	 * @param id
	 **/
	public function getItemAt(id:String):IGroupItem
	{
		var item:IGroupItem;
		for (var i:int = 0; i < _numItem; i++)
		{
			item = _items[i];
			if (item.id == id)
			{
				return item;
			}
		}
		return null;
	}
	
	/**
	 * 毁灭
	 **/
	public function destroy():void
	{
		this._skin = null;
		if (this.parent)
		{
			this.parent.removeChild(this);
		}
		var item:IGroupItem;
		for (var i:int = _numItem - 1; i >= 0; i--)
		{
			item = _items[i];
			item.destroy();
			_items.splice(i, 1);
		}
	}
	
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	
	// 皮肤
	public function set skin(value:ISkin):void
	{
		_skin = value;
		var item:IGroupItem;
		for (var i:int = 0; i < _numItem; i++)
		{
			item = _items[i];
			item.skin = value;
		}
	}	
		
	// 被选中id
	public function set selectedId(value:String):void
	{
		this._selectedId = value;
	}
	
	public function get selectedId():String
	{
		return _selectedId;
	}
	
	// 组件列表
	public function get items():Array
	{
		return _items;
	}

	// 组合数目
	public function get numItem():int
	{
		return _numItem;
	}
	
	// 文本距离组件中心的水平距离
	public function set padding(value:Number):void
	{
		var item:IGroupItem;
		for (var i:int = 0; i < _numItem; i++)
		{
			item = _items[i];
			item.padding = value;
		}
	}
	
	
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	
	
	
	/**
	 * 初始化自身
	 **/
	protected function _init():void
	{
		this.tabChildren = false;
		this.tabEnabled = false;
		this.mouseEnabled = false;
		_items = [];
		_numItem = 0;
	}

	/**
	 * 清除子元素显示
	 **/
	protected function _clear():void
	{
		var len:int = this.numChildren;
		for (var i:int = len - 1; i >= 0; i--)
		{
			removeChildAt(i);
		}
	}
	
	/**
	 * 生成组件项目
	 * @param x
	 * @param y
	 * @param itemSize
	 * @param skin
	 * @param hasLabel
	 **/
	protected function _createItem(x:Number,y:Number,itemSize:Point,skin:ISkin,hasLabel:Boolean):IGroupItem
	{
		// 在子类中实现！
		return null;
	}
	
}

}